﻿package Server.AHMJ.SCMJ;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import Server.AHMJ.SCMJ.HU;
import Server.AHMJ.SCMJ.MajHelper;
import Server.AHMJ.SCMJ.SanMj;
import org.apache.commons.lang.ArrayUtils;

import Server.AHMJ.SCMJ.Constants;
import Server.AHMJ.SCMJ.MajHelper;

public class TDHHelper {

	// （2）碰碰胡：必须已经碰过三次，手中有将牌，再碰一次既可胡牌，这个是两倍。
	public static boolean IsPaiXinDuiDuiHu(int byHandMj[], HU hu, int byMj, int caiShen, ArrayList<HU> list) {
		int OriginalMj[] = new int[Constants.HAND_MJ_MAX];
		Constants.memcpy(OriginalMj, byHandMj, Constants.HAND_MJ_MAX);
		if (byMj != Constants.INVALID_CARD) {
			OriginalMj[Constants.HAND_MJ_MAX - 1] = byMj;
		}
		int cntCS = 0;
		if (caiShen == Constants.INVALID_CARD) {
			cntCS = 0;
		} else {
			cntCS = Constants.countof(OriginalMj, caiShen, Constants.HAND_MJ_MAX);
		}
		for (int i = 0; i < list.size(); i++) {

			HU hu_s = list.get(i);

			int total = 0;
			for (int k = 0; k < Constants.MAX_BLOCK_NUM; k++) {
				switch (hu_s.block[k].style) {
				case Constants.BLOCK_PENG:
				case Constants.BLOCK_KE:
				case Constants.BLOCK_GANG:
				case Constants.BLOCK_AN_GANG: {
					total++;
				}
				default:
					break;
				}
				switch (hu.block[k].style) {
				case Constants.BLOCK_PENG:
				case Constants.BLOCK_KE:
				case Constants.BLOCK_GANG:
				case Constants.BLOCK_AN_GANG: {
					total++;
				}
				default:
					break;
				}
			}
			if (total == Constants.MAX_BLOCK_NUM && hu_s.sanMj[0] == Constants.INVALID_CARD) {
				return true;
			}

			if (total == Constants.MAX_BLOCK_NUM && cntCS == 2) {
				return true;
			}
		}
		return false;

	}
 
	
    

	public static int IsPaiXinQiDuiZi(int byHandMj[], HU hu, int byMj, int caiShen, boolean bZiMo) {
		int OriginalMj[] = new int[Constants.HAND_MJ_MAX];
		Constants.memcpy(OriginalMj, byHandMj, Constants.HAND_MJ_MAX);
		if (byMj != Constants.INVALID_CARD) {
			OriginalMj[Constants.HAND_MJ_MAX - 1] = byMj;
		}
		MajHelper.SortCard(OriginalMj, Constants.HAND_MJ_MAX);
		int cntCS = 0;
		if (caiShen == Constants.INVALID_CARD) {
			cntCS = 0;
		} else {
			cntCS = Constants.countof(OriginalMj, caiShen, Constants.HAND_MJ_MAX);
		}
		Constants.replace(OriginalMj, caiShen, Constants.INVALID_CARD);
		MajHelper.SortCard(OriginalMj, Constants.HAND_MJ_MAX);
		int MjDuiZiCount = 0;
		for (int i = 0; i < Constants.HAND_MJ_MAX - 1; i++) {

			if (OriginalMj[i] != Constants.INVALID_CARD) {// 不是财神
				if (OriginalMj[i] == OriginalMj[i + 1]) {
					MjDuiZiCount++;
					i++;
				}
			}
		}

		int cntHaoHuaDui = 0;
		for (int i = 0; i < Constants.HAND_MJ_MAX - 3; i++) {

			if (OriginalMj[i] != Constants.INVALID_CARD) {
				if (OriginalMj[i] == OriginalMj[i + 1] && OriginalMj[i] == OriginalMj[i + 2]
						&& OriginalMj[i] == OriginalMj[i + 3]) {
					cntHaoHuaDui++;
				}
			}
		}
		MajHelper.SortCard(OriginalMj, Constants.HAND_MJ_MAX);
		if (MjDuiZiCount == 7) {
			return Constants.QI_DUI_ZI;// 清七对
		}
		if (MjDuiZiCount + cntCS >= 7) {
			return Constants.QI_DUI_ZI;// 清七对
		
		}
		return 0;
	}

	// （7）清一色：分为有财神和无财神；“筒”、“万”、“条”其中的任意一种。有财神的是二倍，没财神的是四倍。
	public static int IsPaiXinQingYiSe(int byHandMj[], HU hu, int byMj, int caiShen, ArrayList<HU> list) {
		int OriginalMj[] = new int[Constants.HAND_MJ_MAX];
		Constants.memcpy(OriginalMj, byHandMj, Constants.HAND_MJ_MAX);
		if (byMj != Constants.INVALID_CARD) {
			OriginalMj[Constants.HAND_MJ_MAX - 1] = byMj;
		}

		int cntCS = 0;
		if (caiShen == Constants.INVALID_CARD) {
			cntCS = 0;
		} else {
			cntCS = Constants.countof(OriginalMj, caiShen, Constants.HAND_MJ_MAX);
		}
		Constants.replace(OriginalMj, caiShen, Constants.INVALID_CARD);
		// Constants.replace(OriginalMj, bai, caiShen);
		MajHelper.SortCard(OriginalMj, Constants.HAND_MJ_MAX);

		int iWanCount = 0, iSuoCount = 0, iTongCount = 0;
		for (int i = 0; i < Constants.MAX_BLOCK_NUM; i++) {
			if (hu.block[i].style != Constants.BLOCK_NULL) {
				if (MajHelper.IsWan(hu.block[i].first)) {
					iWanCount += 3;
				}
				if (MajHelper.IsTong(hu.block[i].first)) {
					iTongCount += 3;
				}
				if (MajHelper.IsSuo(hu.block[i].first)) {
					iSuoCount += 3;
				}
				if (MajHelper.IsFeng(hu.block[i].first)) {
					return 0;
				}
			}
		}

		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			if (OriginalMj[i] != Constants.INVALID_CARD) {
				if (OriginalMj[i] >= 0 && OriginalMj[i] <= 8) {
					iWanCount++;
				} else if (OriginalMj[i] >= 9 && OriginalMj[i] <= 17) {
					iTongCount++;
				} else if (OriginalMj[i] >= 18 && OriginalMj[i] <= 26) {
					iSuoCount++;
				} else if (OriginalMj[i] > 26 && OriginalMj[i] <= 33) {
					return 0;
				}
			}
		}
		if (iWanCount == Constants.HAND_MJ_MAX || iSuoCount == Constants.HAND_MJ_MAX
				|| iTongCount == Constants.HAND_MJ_MAX) {
			return Constants.QING_YI_SE;
		}
		if (iWanCount + cntCS == Constants.HAND_MJ_MAX || iSuoCount + cntCS == Constants.HAND_MJ_MAX
				|| iTongCount + cntCS == Constants.HAND_MJ_MAX) {
			return Constants.QING_YI_SE;
		}
		if (iWanCount > 0) {
			if (iWanCount + cntCS == Constants.HAND_MJ_MAX) {
				return Constants.QING_YI_SE;
			}
			return 0;
		}
		if (iTongCount > 0) {

			if (iTongCount + cntCS == Constants.HAND_MJ_MAX) {

				return Constants.QING_YI_SE;

			}
			return 0;
		}
		if (iSuoCount > 0) {

			if (iSuoCount + cntCS == Constants.HAND_MJ_MAX) {
				return Constants.QING_YI_SE;
			}
			return 0;
		}

		return 0;
	}

	// （7）清一色：分为有财神和无财神；“筒”、“万”、“条”其中的任意一种。有财神的是二倍，没财神的是四倍。
//	public static int IsPaiXinHunYiSe(int byHandMj[], HU hu, int byMj, int caiShen, ArrayList<HU> list) {
//		int OriginalMj[] = new int[Constants.HAND_MJ_MAX];
//		Constants.memcpy(OriginalMj, byHandMj, Constants.HAND_MJ_MAX);
//		if (byMj != Constants.INVALID_CARD) {
//			OriginalMj[Constants.HAND_MJ_MAX - 1] = byMj;
//		}
//
//		int cntCS = 0;
//		if (caiShen == Constants.INVALID_CARD) {
//			cntCS = 0;
//		} else {
//			cntCS = Constants.countof(OriginalMj, caiShen, Constants.HAND_MJ_MAX);
//		}
//		Constants.replace(OriginalMj, caiShen, Constants.INVALID_CARD);
//		// Constants.replace(OriginalMj, bai, caiShen);
//		MajHelper.SortCard(OriginalMj, Constants.HAND_MJ_MAX);
//
//		int iWanCount = 0, iSuoCount = 0, iTongCount = 0, iFengCount = 0;
//		for (int i = 0; i < Constants.MAX_BLOCK_NUM; i++) {
//			if (hu.block[i].style != Constants.BLOCK_NULL) {
//				if (MajHelper.IsWan(hu.block[i].first)) {
//					iWanCount += 3;
//				}
//				if (MajHelper.IsTong(hu.block[i].first)) {
//					iTongCount += 3;
//				}
//				if (MajHelper.IsSuo(hu.block[i].first)) {
//					iSuoCount += 3;
//				}
//				if (MajHelper.IsFeng(hu.block[i].first)) {
//					iFengCount += 3;
//				}
//			}
//		}
//
//		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
//			if (OriginalMj[i] != Constants.INVALID_CARD) {
//				if (OriginalMj[i] >= 0 && OriginalMj[i] <= 8) {
//					iWanCount++;
//				} else if (OriginalMj[i] >= 9 && OriginalMj[i] <= 17) {
//					iTongCount++;
//				} else if (OriginalMj[i] >= 18 && OriginalMj[i] <= 26) {
//					iSuoCount++;
//				} else if (OriginalMj[i] > 26 && OriginalMj[i] <= 33) {
//					iFengCount++;
//				}
//			}
//		}
//		if(iFengCount == 0)
//		{
//			return 0;
//		}
//		if (iWanCount+iFengCount == Constants.HAND_MJ_MAX || iSuoCount+iFengCount == Constants.HAND_MJ_MAX
//				|| iTongCount+iFengCount == Constants.HAND_MJ_MAX) {
//			return Constants.HUN_YI_SE;
//		}
//		if (iWanCount + cntCS+iFengCount == Constants.HAND_MJ_MAX || iSuoCount + cntCS+iFengCount == Constants.HAND_MJ_MAX
//				|| iTongCount + cntCS+iFengCount == Constants.HAND_MJ_MAX) {
//			return Constants.HUN_YI_SE;
//		}
//		if (iWanCount > 0) {
//			if (iWanCount + cntCS+iFengCount == Constants.HAND_MJ_MAX) {
//				return Constants.HUN_YI_SE;
//			}
//			return 0;
//		}
//		if (iTongCount > 0) {
//
//			if (iTongCount + cntCS+iFengCount == Constants.HAND_MJ_MAX) {
//
//				return Constants.HUN_YI_SE;
//			}
//			return 0;
//		}
//		if (iSuoCount > 0) {
//
//			if (iSuoCount + cntCS+iFengCount == Constants.HAND_MJ_MAX) {
//				return Constants.HUN_YI_SE;
//			}
//			return 0;
//		}
//
//		return 0;
//	}

	// 国士无双
	public static boolean IsGuoShiWuShuang(int byHandMj[], int byMj, int caiShen) {
		if (byHandMj[Constants.HAND_MJ_MAX - 2] == Constants.INVALID_CARD) {
			return false;// 手牌必须13(放统) 或者 14张(自摸) 不能吃碰杠
		}

		int OriginalMj[] = new int[Constants.HAND_MJ_MAX];
		Constants.memcpy(OriginalMj, byHandMj, Constants.HAND_MJ_MAX);
		if (byMj != Constants.INVALID_CARD) {
			OriginalMj[Constants.HAND_MJ_MAX - 1] = byMj;
		}
		MajHelper.SortCard(OriginalMj, Constants.HAND_MJ_MAX);

		int cntCS = 0;
		if (caiShen == Constants.INVALID_CARD) {
			cntCS = 0;
		} else {
			cntCS = Constants.countof(OriginalMj, caiShen, Constants.HAND_MJ_MAX);
		}
		Constants.replace(OriginalMj, caiShen, Constants.INVALID_CARD);

		int gswsMj[] = { 0, 8, 9, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33, Constants.INVALID_CARD };

		int cntMjMj2 = 0;
		int cntMj_gsws = 0;
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			if (OriginalMj[i] != Constants.INVALID_CARD) {
				int cntMj = Constants.countof(gswsMj, OriginalMj[i], Constants.HAND_MJ_MAX);
				if (cntMj > 2 || cntMj == 0) {
					return false;
				} else {
					if (cntMj == 2) {
						cntMjMj2++;
					}
					cntMj_gsws++;
				}

			}
		}
		if (cntMjMj2 > 1) {
			return false;
		}

		if (cntMj_gsws + cntCS == 14) {
			return true;
		}
		return false;
	}
	
	//判断妖头
	public static boolean IsYaoTou(int byHandMj[], int byMj, int caiShen) {
		int OriginalMj[] = new int[Constants.HAND_MJ_MAX];
		Constants.memcpy(OriginalMj, byHandMj, Constants.HAND_MJ_MAX);
		if (byMj != Constants.INVALID_CARD) {
			OriginalMj[Constants.HAND_MJ_MAX - 1] = byMj;
		}
		MajHelper.SortCard(OriginalMj, Constants.HAND_MJ_MAX);
		  int fengCount=0;
		 for(int i=0;i<OriginalMj.length;i++){
			 if((31>=OriginalMj[i]&&OriginalMj[i]<=34)&&OriginalMj[i]!=caiShen){
				 fengCount++;
			 }
		 }
		if(fengCount==2){
			return true;
		}
		return false;
		
	}
	//判断缺一门，宝牌也算花色
	 
	//跑中的
	

	
}
